package com.eastday.model;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;

import com.eastday.util.ParseConfigFactory;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.SimpleSequence;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class NewsPrintModel {

	private List<NewsInfoModel> newsInfoModeList;
	private StringBuffer htmlContent;
	private String exportPath;
	private int isIndexPage;//是否是首页
	
	public NewsPrintModel(){
		htmlContent = new StringBuffer();
		exportPath = ParseConfigFactory.sysConfigModel.getExportPath();
		File directory = new File(exportPath);
		if(!directory.exists())
		{
			directory.mkdirs();
		}
	}

	public List<NewsInfoModel> getNewsInfoModeList() {
		return newsInfoModeList;
	}

	public void setNewsInfoModeList(List<NewsInfoModel> newsInfoModeList) {
		this.newsInfoModeList = newsInfoModeList;
	}

	public StringBuffer getHtmlContent() {
		return htmlContent;
	}

	public void setHtmlContent(StringBuffer htmlContent) {
		this.htmlContent = htmlContent;
	}
	

	public int getIsIndexPage() {
		return isIndexPage;
	}

	public void setIsIndexPage(int isIndexPage) {
		this.isIndexPage = isIndexPage;
	}

//	public void convertArrayToString(String filename,int maxPage){
//		
//		File file = null;
//		htmlContent.append("<div>\n");
//		htmlContent.append("<ul>\n");
//		for(NewsInfoModel newsInfoModel:this.newsInfoModeList)
//		{
//			htmlContent.append("<li>");
//			htmlContent.append("<span>["+newsInfoModel.getChannel()+"]</span>");
//			htmlContent.append("<a href='"+newsInfoModel.getLink()+"'>");
//			htmlContent.append(newsInfoModel.getTopic());
//			htmlContent.append("</a>");
//			htmlContent.append("<span>");
//			htmlContent.append(newsInfoModel.getDatetime());
//			htmlContent.append("</span>");
//			htmlContent.append("</li>\n");
//		}
//		htmlContent.append("</ul>\n");
//		htmlContent.append("<p>\n");
//		if(isIndexPage == 0)//如果不是首页
//		{
//			int prevPage = maxPage+1;
//			int nextPage = maxPage-1;
//			htmlContent.append("<a href='./"+filename+prevPage+".html'>上一页</a>&nbsp;");
//			if(nextPage!=0)
//			{
//				htmlContent.append("<a href='./"+filename+nextPage+".html'>下一页</a>");
//			}
//			file = new File(exportPath+"/"+filename+maxPage+".html");
//		}
//		else
//		{
//			for(int i=0,j=maxPage+1;i<10;i++)
//			{
//				htmlContent.append("<a href='./"+filename+j+".html'>"+(i+1)+"<a>&nbsp;\n");
//				j--;
//				if(j<=0)
//					break;
//			}
//			if(isIndexPage == 1)//首页
//			{
//				file = new File(exportPath+"/"+filename+".html");
//			}
//			else//首页副本
//			{
//				maxPage++;
//				file = new File(exportPath+"/"+filename+maxPage+".html");
//			}
//		}
//		
//		htmlContent.append("</p>\n");
//		htmlContent.append("</div>");
//		FileOutputStream outputStream = null;
//		BufferedWriter bw = null;
//		try
//		{
//			outputStream = new FileOutputStream(file);
//			bw = new BufferedWriter(new OutputStreamWriter(outputStream));
//			bw.write(htmlContent.toString());
//		}
//		catch(IOException e)
//		{
//			e.printStackTrace();
//		}
//		finally
//		{
//			try {
//				bw.flush();
//				bw.close();
//				outputStream.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		
//		
//		
//	}
	
	
	public void convertArrayToString(String filename,int maxPage) throws IOException, TemplateException{
		
		Configuration cfg  = new Configuration();
		cfg.setDirectoryForTemplateLoading(new File(ParseConfigFactory.sysConfigModel.getTemplatedir()));
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		Template template  = cfg.getTemplate(ParseConfigFactory.sysConfigModel.getTemplatefile());
		Map root           = new HashMap();
		SimpleSequence newseq = new SimpleSequence();
		for(NewsInfoModel newsInfoModel:this.newsInfoModeList)
		{
			Map newsinfo   = new HashMap();
			newsinfo.put("channel", newsInfoModel.getChannel());
			newsinfo.put("datetime", newsInfoModel.getDatetime());
			newsinfo.put("link", newsInfoModel.getLink());
			newsinfo.put("topic", newsInfoModel.getTopic());
			newseq.add(newsinfo);
		}
		root.put("newseq", newseq);
		String exporthtmlname = "";
		String pagenum = "";
		if(isIndexPage == 0)//如果不是首页
		{
			int prevPage = maxPage+1;
			int nextPage = maxPage-1;
			pagenum     += "<a href='./"+filename+prevPage+".html'>上一页</a>&nbsp;";
			if(nextPage!=0)
			{
				pagenum +="<a href='./"+filename+nextPage+".html'>下一页</a>";
			}
			exporthtmlname = exportPath+"/"+filename+maxPage+".html";
		}
		else
		{
			for(int i=0,j=maxPage+1;i<10;i++)
			{
				String pagenumsingle = "<a href='./"+filename+j+".html'>"+(i+1)+"<a>&nbsp;\n";
				pagenum             += pagenumsingle;
				j--;
				if(j<=0)
					break;
			}
			if(isIndexPage == 1)//首页
			{
				exporthtmlname = exportPath+"/"+filename+".html";
			}
			else//首页副本
			{
				maxPage++;
				exporthtmlname = exportPath+"/"+filename+maxPage+".html";
			}
		}
		root.put("pagenum", pagenum);
		Writer out = new OutputStreamWriter(new FileOutputStream(exporthtmlname),"UTF-8");
		template.process(root, out);
		out.flush();
	}
	
	
	
	
	
}
